package com.bzkj.basicinfo.service.fieldTranslation.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bzkj.basicinfo.domain.fieldTranslation.FieldTranslation;
import com.bzkj.basicinfo.mapper.FieldTranslationMapper;
import com.bzkj.basicinfo.service.fieldTranslation.IFieldTranslationService;
import com.bzkj.common.core.domain.AjaxResult;
import com.bzkj.generator.domain.GenTable;
import com.bzkj.generator.domain.GenTableColumn;
import com.bzkj.generator.mapper.GenTableColumnMapper;
import com.bzkj.generator.mapper.GenTableMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 字段翻译映射Service业务层处理
 *
 * @author bzkj
 * @date 2025-03-21
 */
@Service
public class FieldTranslationServiceImpl implements IFieldTranslationService {
    @Resource
    private FieldTranslationMapper fieldTranslationMapper;

    @Resource
    private GenTableColumnMapper genTableColumnMapper;

    @Resource
    private GenTableMapper genTableMapper;

    /**
     * 查询字段翻译映射
     *
     * @param id 字段翻译映射主键
     * @return 字段翻译映射
     */
    @Override
    public FieldTranslation selectFieldTranslationById(Long id) {
        return fieldTranslationMapper.selectFieldTranslationById(id);
    }

    /**
     * 查询字段翻译映射列表
     *
     * @param fieldTranslation 字段翻译映射
     * @return 字段翻译映射
     */
    @Override
    public List<FieldTranslation> selectFieldTranslationList(FieldTranslation fieldTranslation) {
        return fieldTranslationMapper.selectFieldTranslationList(fieldTranslation);
    }

    /**
     * 新增字段翻译映射
     *
     * @param fieldTranslation 字段翻译映射
     * @return 结果
     */
    @Override
    public int insertFieldTranslation(FieldTranslation fieldTranslation) {
        return fieldTranslationMapper.insertFieldTranslation(fieldTranslation);
    }

    /**
     * 修改字段翻译映射
     *
     * @param fieldTranslation 字段翻译映射
     * @return 结果
     */
    @Override
    public int updateFieldTranslation(FieldTranslation fieldTranslation) {
        return fieldTranslationMapper.updateFieldTranslation(fieldTranslation);
    }

    /**
     * 批量删除字段翻译映射
     *
     * @param ids 需要删除的字段翻译映射主键
     * @return 结果
     */
    @Override
    public int deleteFieldTranslationByIds(Long[] ids) {
        return fieldTranslationMapper.deleteFieldTranslationByIds(ids);
    }

    /**
     * 删除字段翻译映射信息
     *
     * @param id 字段翻译映射主键
     * @return 结果
     */
    @Override
    public int deleteFieldTranslationById(Long id) {
        return fieldTranslationMapper.deleteFieldTranslationById(id);
    }

    /**
     * 根据表名获取表字段信息
     *
     * @param tableName 表名
     * @return 查询结果
     */
    @Override
    public AjaxResult getTableInfoAndFieldInfo(String tableName) {
        HashMap<String, Object> dataMap = new HashMap<>(2);
        List<GenTable> genTables = genTableMapper.selectDbTableListByNames(new String[]{tableName});
        if (!genTables.isEmpty()) {
            GenTable genTable = genTables.get(0);
            dataMap.put("dbTableInfo", genTable);
            List<GenTableColumn> genTableColumns = genTableColumnMapper.selectDbTableColumnsByName(tableName);
            dataMap.put("dbTableColumns", genTableColumns);
        } else {
            return AjaxResult.error("表不存在!");
        }
        return AjaxResult.success(dataMap);
    }

    /**
     * 导入字段翻译映射
     *
     * @param resultJsonObject 导入数据
     * @return 导入结果
     */
    @Override
    public AjaxResult importFieldTranslation(JSONObject resultJsonObject) {
        // 表名
        String tableName = resultJsonObject.getString("tableName");
        int count = 0;
        // 获取字段信息
        JSONArray jsonArray = resultJsonObject.getJSONArray("fields");
        for (int i = 0; i < jsonArray.size(); i++) {
            HashMap<String, Object> jsonObject = jsonArray.getJSONObject(i);
            jsonObject.put("tableName", tableName);
            count += fieldTranslationMapper.importFieldTranslation(jsonObject);

        }
        if (count > 0) {
            return AjaxResult.success("导入成功!");
        }
        return AjaxResult.error("导入失败！！");
    }
}
